summaryrefslogtreecommitdiff
path: root/src/pages/shop/brands/[slug].js
blob: 320c445451d74f586a6e7b9f3455c3ff6bb516ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import axios from "axios";
import { useState } from "react";
import Filter from "../../../components/Filter";
import Header from "../../../components/Header";
import Layout from "../../../components/Layout";
import Pagination from "../../../components/Pagination";
import ProductCard from "../../../components/ProductCard";
import { getNameFromSlug } from "../../../helpers/slug";
import FilterIcon from "../../../icons/filter.svg";

export async function getServerSideProps(context) {
  const { 
    slug, 
    page = 1, 
    category = '',
    price_from = '',
    price_to = '',
    order_by = '',
  } = context.query;

  let urlParameter = [
    'q=*',
    `page=${page}`,
    `brand=${getNameFromSlug(slug)}`,
    `category=${category}`,
    `price_from=${price_from}`,
    `price_to=${price_to}`,
    `order_by=${order_by}`
  ].join('&');
  let searchResults = await axios(`${process.env.SELF_HOST}/api/shop/search?${urlParameter}`);
  searchResults = searchResults.data;
  
  return {
    props: { 
      searchResults, 
      page, 
      slug, 
      category, 
      price_from,
      price_to,
      order_by
    }
  };
}

export default function BrandDetail({ 
  searchResults,
  page,
  slug,
  category, 
  price_from,
  price_to,
  order_by
}) {
  const pageCount = Math.ceil(searchResults.response.numFound / searchResults.responseHeader.params.rows);
  const productStart = searchResults.responseHeader.params.start;
  const productRows = searchResults.responseHeader.params.rows;
  const productFound = searchResults.response.numFound;

  const [activeFilter, setActiveFilter] = useState(false);

  const route = () => {
    let route = `/shop/brands/${slug}`;
    if (category) route += `&category=${category}`;
    if (price_from) route += `&price_from=${price_from}`;
    if (price_to) route += `&price_to=${price_to}`;
    if (order_by) route += `&order_by=${order_by}`;
    return route;
  }

  return (
    <>
      <Header title={`Distributor ${getNameFromSlug(slug)} Indonesia Harga Official - Indoteknik`} />
      <Filter 
        defaultRoute={`/shop/brands/${slug}`}
        isActive={activeFilter}
        closeFilter={() => setActiveFilter(false)}
        defaultPriceFrom={price_from}
        defaultPriceTo={price_to}
        defaultBrand=''
        defaultCategory={category}
        defaultOrderBy={order_by}
        searchResults={searchResults}
        disableFilter={['brand']}
      />
      <Layout>
        <div className="p-4">
          <button className="btn-light py-2 flex items-center gap-x-2 mb-2" onClick={() => setActiveFilter(true)}>
            <FilterIcon className="w-4 h-4" /> <span>Filter</span>
          </button>
          <h1>Produk</h1>
          <div className="text-caption mb-4">
            {productFound > 0 ? (
              <>
                Menampilkan&nbsp;
                {pageCount > 1 ? (
                  <>
                    {productStart + 1}-{
                      (productStart + productRows) > productFound ? productFound : productStart + productRows
                    }
                    &nbsp;dari&nbsp;
                  </>
                ) : ''}
                {searchResults.response.numFound}
                &nbsp;produk untuk brand <span className="font-semibold">{getNameFromSlug(slug)}</span>
              </>
            ) : 'Mungkin yang anda cari'}
          </div>
          <div className="grid grid-cols-2 gap-3">
            {searchResults.response.products.map((product) => (
              <ProductCard key={product.id} data={product} />
            ))}
          </div>
          
          <div className="mt-4">
            <Pagination pageCount={pageCount} currentPage={parseInt(page)} url={route()} />
          </div>
        </div>
      </Layout>
    </>
  )
}